package com.dhcc.wb.gongzhonghao;

import java.util.List;

import org.apache.log4j.Logger;

import com.dhcc.common.database.DBManager;
import com.dhcc.common.util.StringUtil;

public class GzhDao {
	private static final Logger logger = Logger.getLogger(GzhDao.class);
	
		/*
		 * 查询所有
		 */
		@SuppressWarnings("unchecked")
		public List<GzhModel> getAll() {
			DBManager dbm = null;
			List<GzhModel> menuModel = null;
			try {
				dbm = new DBManager();
				String sql = "SELECT * from weixin_gongzhonghao order by qiyeid,gzhname";
				menuModel = dbm.getObjectList(GzhModel.class, sql);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				dbm.close();
			}
			return menuModel;
		}
	
		public Integer queryCount(String name) {
			DBManager dbm = null;
			Integer count = null;
			try {
				dbm = new DBManager();
				String sql = "select count(*) from weixin_gongzhonghao g where 1=1 ";
				if(!StringUtil.isNullOrEmpty(name)){
					sql += "and g.gzhname like '%"+ name +"%'";
				}
				count = dbm.executeQueryCount(sql);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				dbm.close();
			}
			return count;
		}
	
	
	
	
		/*
		 * 查询
		 */
		@SuppressWarnings("unchecked")
		public List<GzhModel> findQiyeServiceByPage(Integer pageNumber,Integer pageSize,String name) {
			DBManager dbm = null;
			List<GzhModel> list = null;
			try {
				dbm = new DBManager();
				String sql = "select * from weixin_gongzhonghao where 1=1 ";
					   if(!StringUtil.isNullOrEmpty(name)){
						   sql += " and gzhname LIKE '%"+ name +"%'";			   
					   }
					   sql += " order by  qiyeid,gzhname";
				       sql +=  " limit " + pageNumber + ","+ pageSize +"";
				list = dbm.getObjectList(GzhModel.class, sql);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				dbm.close();
			}
			return list;
		}
	
		//删除
		public int delete(String _id) {
			int i =0;
			DBManager dbm = null;
			try{
				dbm = new DBManager();
				String sql = "delete from weixin_gongzhonghao where gzhid = '"+ _id +"'";
				i = dbm.executeUpdate(sql);
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				dbm.close();
			}
			return i;
		}
		
		//编辑
		public GzhModel queryqiye_result(String _id) {
			GzhModel model = null;
			DBManager dbm = null;
			try{
				dbm = new DBManager();
				String sql = "select * from weixin_gongzhonghao qiye  where qiye.gzhid = '"+ _id +"'";
				model = (GzhModel) dbm.getObject(GzhModel.class, sql);
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				dbm.close();
			}
			return model;
		}
		
		
		//更新token
		public int updateGzh(String gzhid, String access_token,Long creattime,Long daoqitime,Integer expire_in) {
			int i = 0;
			DBManager dbm = null;
			try{
				dbm = new DBManager();
				String sql = "update weixin_gongzhonghao set access_token = '"+ access_token +"',creattime='"+creattime+"',daoqitime='"+daoqitime+"',expire_in='"+expire_in+"' where gzhid = '"+ gzhid +"'";
				i = dbm.executeUpdate(sql);
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				dbm.close();
			}
			return i;
		}
		
		
		
		//新增
		public int addGzh(String gzhid, String gzhname,String appid,String secret,String qiyeid,String beizhu,String qiyename) {
			int i=0;
			DBManager dbm = null;
			try{
				dbm = new DBManager();
				String sql = "insert into weixin_gongzhonghao(gzhid,gzhname,appid,secret,qiyeid,beizhu,qiyename) values('"+ gzhid +"','"+ gzhname +"','"+ appid +"','"+secret+"','"+qiyeid+"','"+beizhu+"','"+qiyename+"')";
				i = dbm.executeUpdate(sql);
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				dbm.close();
			}
			return i;
		}
		
		//更新token
		public int updateGzhEnt(String gzhid, String gzhname,String appid,String secret,String qiyeid,String beizhu,String qiyename) {
			int i = 0;
			DBManager dbm = null;
			try{
				dbm = new DBManager();
				String sql = "update weixin_gongzhonghao set gzhname = '"+ gzhname +"',appid='"+appid+"',secret='"+secret+"',qiyeid='"+qiyeid+"',beizhu='"+beizhu+"',qiyename='"+qiyename+"' where gzhid = '"+ gzhid +"'";
				i = dbm.executeUpdate(sql);
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				dbm.close();
			}
			return i;
		}
		//根据企业id获取公众号列表。
		public int interGzhByqiyeCount(String qiyeid) {
			int i = 0;
			DBManager dbm = null;
			try{
				dbm = new DBManager();
				String sql = "select count(*) from weixin_gongzhonghao t where t.qiyeid = '"+qiyeid+"'";
				i = dbm.executeQueryCount(sql);
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				dbm.close();
			}
			return i;
		}
		
		//根据企业id获取公众号列表。
		public int interGzhByqiyeNameCount(String qiyename) {
			int i = 0;
			DBManager dbm = null;
			try{
				dbm = new DBManager();
				String sql = "select count(*) from weixin_gongzhonghao t where t.qiyename = '"+qiyename+"'";
				i = dbm.executeQueryCount(sql);
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				dbm.close();
			}
			return i;
		}
		
		//根据企业id获取公众号列表。
		@SuppressWarnings("unchecked")
		public List<GzhModel> getGzhByqiyeNameCount(String qiyename) {
			List<GzhModel> list = null;
			DBManager dbm = null;
			try{
				dbm = new DBManager();
				String sql = "select * from weixin_gongzhonghao  where 1=1 ";
				if(!StringUtil.isNullOrEmpty(qiyename)){
					sql += " and  qiyename = '"+qiyename+"'";
				}
				list = dbm.getObjectList(GzhModel.class, sql);
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				dbm.close();
			}
			return list;
		}
				
		
		/*
		 * 查询
		 * 输入一个参数 可能是企业名称，可能是公众号名称
		 */
		@SuppressWarnings("unchecked")
		public List<GzhModel> findGzhByQiyeOrGzhname(String name) {
			DBManager dbm = null;
			List<GzhModel> list = null;
			try {
				dbm = new DBManager();
				String sql = "select * from weixin_gongzhonghao where 1=1 ";
					   if(!StringUtil.isNullOrEmpty(name)){
						   sql += " and (gzhname like '%"+ name +"%' or qiyename like '%"+name+"%')";			   
					   }
					   sql += " order by  qiyename,gzhname";
				list = dbm.getObjectList(GzhModel.class, sql);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				dbm.close();
			}
			return list;
		}
		
		/*
		 * 查询
		 * 
		 * 
		 * SELECT t.`qiyeid`,t.`qiyename`,COUNT(*) AS counts FROM weixin_gongzhonghao t WHERE t.qiyeid='1' GROUP BY t.`qiyeid` ORDER BY counts  DESC
		 */
		@SuppressWarnings("unchecked")
		public List<GzhModel> fingByQiyeid(String qiyeid) {
			DBManager dbm = null;
			List<GzhModel> list = null;
			try {
				dbm = new DBManager();
//				String sql = "select t.qiyeid,t.qiyename,count(*) as expire_in from weixin_gongzhonghao t where 1=1 ";
				String sql = "select t.qiyename,count(*) as expire_in from weixin_gongzhonghao t where 1=1 ";
					   if(!StringUtil.isNullOrEmpty(qiyeid)){
						   sql += " and qiyeid = '"+qiyeid+"'";	   
					   }
					   sql += " group by t.qiyename order by expire_in desc";
				list = dbm.getObjectList(GzhModel.class, sql);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				dbm.close();
			}
			return list;
		}
		
		/*
		 * SELECT * FROM weixin_user t WHERE t.`ref_date`='2019-11-09' AND t.`gzhid`='1'   ORDER BY t.`cumulate_user` DESC
		 */
		@SuppressWarnings("unchecked")
		public List<GzhModel> fingByQiyeid2(String qiyeid) {
			DBManager dbm = null;
			List<GzhModel> list = null;
			try {
				dbm = new DBManager();
				String sql = "select *  from weixin_gongzhonghao t where 1=1 ";
					   if(!StringUtil.isNullOrEmpty(qiyeid)){
						   sql += " and qiyeid = '"+qiyeid+"'";			   
					   }
				list = dbm.getObjectList(GzhModel.class, sql);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				dbm.close();
			}
			return list;
		}
		
		/*
		 * SELECT * FROM weixin_user t WHERE t.`ref_date`='2019-11-09' AND t.`gzhid`='1'   ORDER BY t.`cumulate_user` DESC
		 */
		@SuppressWarnings("unchecked")
		public List<GzhModel> fingByQiyeName(String qiyename) {
			DBManager dbm = null;
			List<GzhModel> list = null;
			try {
				dbm = new DBManager();
				String sql = "select *  from weixin_gongzhonghao t where 1=1 ";
					   if(!StringUtil.isNullOrEmpty(qiyename)){
						   sql += " and qiyename = '"+qiyename+"'";			   
					   }
				list = dbm.getObjectList(GzhModel.class, sql);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				dbm.close();
			}
			return list;
		}
		
		
}


